<?php
/**
 * 管理员后台
 * User: henrick
 * Date: 2017/3/22
 * Time: 18:17
 */
namespace app\core;

use think\Controller;

class Incontroller extends Controller
{
    protected $no_auth_url = [   //不用权限校验的url
        'admin/index/login',
        'admin/index/reg',
        'admin/index/page403',
        'admin/index/logout'
    ];

    protected $login_no_auth_url = [
        'admin/api/getqiniuuploadtoken',  //上传七牛云token
        'admin/api/delqiniures',          //七牛云资源删除接口
    ];

    protected function _initialize(){
        //echo url('admin/index/login',['user'=>1]);
        $this->_routerAuth();
    }

    protected function _routerAuth(){  //权限校验
        $url = url('','',false);
        $url = ltrim(str_replace('/index.php/','',$url),'/');

        //判断url是否是为不用权限校验
        if(in_array($url,$this->no_auth_url)){
            return true;
        }

        $admin_info = session('admin_user_info');

        //判断是否登录
        $res = $this->_isLogin();
        if(!$res){
            $this->redirect(url('admin/index/login'));
            exit;
        }

        if(in_array($url,$this->login_no_auth_url)){
            return true;
        }

        //判断是否有权限访问
        $auth_list = cache('cache_admin_user_id_'.$admin_info['id']);
        if(in_array($url,$auth_list['user_auth'])){
            unset($auth_list,$admin_info);
            return true;
        }

        $this->redirect(url('admin/index/page403'));
        exit;
    }

    protected function _isLogin(){   //是否登录
        $admin_info = session('admin_user_info');
        if(!$admin_info){
            return false;
        }
        return true;
    }
}